/*
 * @Author: NtyAaron
 * @Date: 2021-04-12 11:13:45
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-06-16 14:09:24
 * @Description: file content
 * @FilePath: \dwq\pages\check\checkRailway\checkRailway.js
 */
const app = getApp()
import { getCity, getDefCity } from '../../../utils/cityJson'
import { getPosterList } from '../../../api/advert/advert'
import { formatDate } from '../../../utils/util'
import { _t } from '../../../i18n/base'
import { getLangCityName } from '../../../utils/cityJson'
Page({
  data: {
    //弹窗
    lang: {},
    showDialog: false,
    showDatePicker: false,
    fromCity: '',
    toCity: '',
    type: 'from',
    language: 1,
    currentDateStr: formatDate(new Date()),
    currentDate: JSON.stringify(new Date()),
    cityParse: {},
    columnsCity: [],
    getAdvertObj: {
      categoryType: '',
      language: '',
      posterName: '',
      posterType: 8,
      showType: 1,
      trafficType: 2,
    },
    bannerList: [],
  },
  // 广久直通车
  guangZhouTowloon() {
    wx.navigateTo({
      url: '../guangZhouToKowloon/guangZhouToKowloon',
    })
  },
  // 选择城市
  chooseCity(e) {
    this.setData({
      showDialog: true,
      type: e.currentTarget.dataset.type,
    })
  },
  // 关闭弹窗
  onCancelCity() {
    this.setData({
      showDialog: false,
    })
  },
  // 确认城市
  onConfirmCity({ detail }) {
    const { picker, value, index } = detail
    if (this.data.type == 'from') {
      this.setData({
        fromCity: value[1],
        showDialog: false,
      })
    } else {
      this.setData({
        toCity: value[1],
        showDialog: false,
      })
    }
  },
  // 改变
  onChangeCity({ detail }) {
    const { picker, value, index } = detail
    picker.setColumnValues(1, this.data.cityParse[value[0]])
  },
  // 确认时间
  confirmDate(e) {
    this.setData({
      currentDate: JSON.stringify(e.detail.date),
      currentDateStr: e.detail.date,
      showDatePicker: false,
    })
  },
  // 选择本次时间
  showOrHidden() {
    this.setData({
      showDatePicker: !this.data.showDatePicker,
    })
  },
  // 查询按钮
  check() {
    let reqInfo = {
      currentDate: this.data.currentDate,
      date: this.data.currentDateStr,
      start: getLangCityName(this.data.fromCity, this.data.language, 1),
      end: getLangCityName(this.data.toCity, this.data.language, 1),
      language: this.data.language,
    }
    wx.navigateTo({
      url: `/pages/check/railwayResult/railwayResult?info=${JSON.stringify(reqInfo)}`,
    })
  },
  // 获取广告
  async getPosterList() {
    this.setData({
      'getAdvertObj.language': this.data.language,
    })
    let { result } = await getPosterList(this.data.getAdvertObj)
    if (result) {
      result.map((item) => {
        item.bannerLink = `${app.globalData.host}/attach/showPic?attachId=${item.pic}`
      })
      this.setData({
        bannerList: result,
      })
    }
  },
  // 点击广告
  adClickHandle(e) {
    let link = e.currentTarget.dataset.link
    wx.setClipboardData({
      data: link,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 复制链接地址
  linkCopyHandle(e) {
    // 中铁
    let ZTUrl = {
      zh: 'https://www.12306.cn/index/',
      tw: 'https://www.12306.cn/index/',
      en: 'https://www.12306.cn/en/index.html',
    }
    // 跨境高铁香港段
    let GTUrl = {
      zh: 'https://www.highspeed.mtr.com.hk/sc/ticket/destination.html',
      tw: 'https://www.highspeed.mtr.com.hk/tc/ticket/destination.html',
      en: 'https://www.highspeed.mtr.com.hk/en/ticket/destination.html',
    }
    // 钢铁城际
    let GJUrl = {
      zh: 'https://www.it3.mtr.com.hk/b2c/frmScheduleGuangdong.asp?strLang=GB	',
      tw: 'https://www.it3.mtr.com.hk/b2c/frmScheduleGuangdong.asp?strLang%3DBig5',
      en: 'https://www.it3.mtr.com.hk/b2c/frmScheduleGuangdong.asp?strLang=Eng',
    }
    let type = e.currentTarget.dataset.type
    let language = wx.getStorageSync('language') //1简体2繁体3英文
    let url = ''
    if (type == 1) {
      url = ZTUrl[language]
    } else if (type == 2) {
      url = GTUrl[language]
    } else {
      url = GJUrl[language]
    }
    wx.setClipboardData({
      data: url,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 路线规划
  routerJump() {
    wx.navigateTo({
      url: '/pages/interactionMap/interactionMap',
    })
  },
  //初始化数据
  initData() {
    const citys = getCity()
    let cityParse = JSON.parse(JSON.stringify(citys))
    delete cityParse['澳門']
    delete cityParse['澳门']
    delete cityParse['香港']
    delete cityParse['台湾']
    delete cityParse['臺灣']
    delete cityParse['Hong Kong']
    delete cityParse['Macao']
    delete cityParse['Taiwan']
    let data = {
      cityParse,
      columnsCity: [
        {
          values: Object.keys(cityParse),
        },
        {
          values: cityParse[getDefCity()],
          defaultIndex: 1,
        },
      ],
    }
    this.setData(data)
  },
  onLoad() {
    let language = wx.getStorageSync('lang')
    this.setData({
      lang: _t(),
      language,
    })
    this.getPosterList()
    wx.setNavigationBarTitle({
      title: this.data.lang['铁路查询'],
    })
    this.initData()
  },
})
